package ru.st.selenium.pages;

import java.util.List;

import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.FindBys;

public class AddFilmPage extends Page{
	
	public AddFilmPage(PageManager pages) {
		super(pages);
	}
	@FindBy(css = "[name='name']")
	private WebElement title;
	
	@FindBy(css = "[name='year']")
	private WebElement year;
	
	@FindBy(id = "submit")
	  private WebElement saveButton;
	
	@FindBys(@FindBy(css = ".required.error"))
	private List<WebElement> errors;
	
	 public AddFilmPage setTitle(String text) {
		    title.sendKeys(text);
		    return this;
		  }

		  public AddFilmPage setYear(String text) {
		    year.sendKeys(text);
		    return this;
		  }
	
	public void clickSaveButton() {
	    saveButton.click();
	  }
	
	public boolean isErrorPresent() {
		try {
			return errors != null && !errors.isEmpty();
		} catch (NoSuchElementException e){
			return false;
		}
	}
	
	public int getErrorsCount(){
		try {
			return errors != null ? errors.size() : 0;
		} catch (NoSuchElementException e){
			return 0;
		}
	}
}
